/*
-----------------------------------------------------------------------------
This source file is part of as4game
    (ActionScript For Game Engine)
For the latest info, see http://as4game.googlecode.com

Copyright (c) 2000-2011 Hproof Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
package org.as4game.common.utils
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	/**
	 * Fps
	 */
	public class Fps
	{
			// 帧时间, 毫秒
		public var time_last:int;			// 上一帧开始时间
		public var time_now:int;			// 当前帧开始时间
		public var time_pass:int;			// 上一帧->当前帧 所经过的时间
		
			// 帧速率
		public var fps:int;
		private var _fps_sec_start:int;
		private var _fps_sec_total:int;
		
			// 帧花费
		public var frameCost:int;
		private var _frame_sec_total:int;
		private var _frame_start:int;
		private var _frame_end:int;
		
			// 更新花费
		public var updateCost:int;
		private var _update_sec_total:int;
		private var _update_start:int;
		private var _update_end:int;
		
			// 渲染花费
		public var renderCost:int;
		private var _render_sec_total:int;
		private var _render_start:int;
		private var _render_end:int;
		
		
		/**
		 * 帧开始
		 */
		public function frameStart():void{
			
			time_last = time_now;
			time_now = getTimer();
			time_pass = time_now - time_last; 
			
			++ _fps_sec_total;
			if( time_now - _fps_sec_start > 1000 ){
				fps = _fps_sec_total;
				_fps_sec_total = 0;
				_fps_sec_start = time_now;
				
				frameCost = _frame_sec_total;
				_frame_sec_total = 0;
				
				updateCost = _update_sec_total;
				_update_sec_total = 0;
				
				renderCost = _render_sec_total;
				_render_sec_total = 0;
			}
			
			_frame_start = time_now;
			_update_start = 0;
			_update_end = 0;
			_render_start = 0;
			_render_end = 0;
		}
		
		/**
		 * 帧结束
		 */
		public function frameEnd():void{
			_frame_end  = getTimer();
			_frame_sec_total += ( _frame_end - _frame_start );
			
			if( _update_start ){
				if( !_update_end ) _update_end = _frame_end
				_update_sec_total += ( _update_end - _update_start );
			}
			
			
			if( _render_start ){
				if( !_render_end ) _render_end = _frame_end;
				_render_sec_total += (_render_end - _render_start );
			}
		}
		
		/**
		 * 更新开始
		 */
		public function updateStart():void{
			_update_start = getTimer();
			if( _render_start ) _render_end = _update_start;
		}
		
		/**
		 * 渲染开始
		 */
		public function renderStart():void{
			_render_start = getTimer();
			if( _update_start ) _update_end = _render_start;
		}
		
		
		/**
		 * 更新所占时间百分比, [0-100]
		 */
		public function get updateRatio():int{
			if( ! frameCost ) return 0;
			return ( updateCost * 100 / frameCost );
		}
		
		///
		public function get frameRatio():int{
			return ( frameCost * 100 / 1000 );
		}
		
		/**
		 * 渲染所占比例
		 */
		public function get renderRatio():int{
			if(! frameCost ) return 0;
			return ( renderCost * 100 / frameCost );
		}
	}
}